/*
 * Copyright (c) 2017 Qunar.com. All Rights Reserved.
 */
package com.guoyu.project.web.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Article;
import com.guoyu.project.model.School;
import com.guoyu.project.model.enums.ArticleType;
import com.guoyu.project.model.enums.CollegeType;
import com.guoyu.project.model.enums.ProjectType;
import com.guoyu.project.service.ArticleService;
import com.guoyu.project.service.SchoolService;
import com.guoyu.project.web.admin.NewsController;

/**
 * @author: libo Date: 17-2-13 Time: 下午1:20
 * @version: \$Id$
 */
@Controller
@RequestMapping("api/school")
public class SchoolApiController {

    private final static Logger logger = LoggerFactory.getLogger(NewsController.class);

    @Resource
    private SchoolService schoolService;
    @Resource
    private ArticleService articleService;

    /**
     * 工程概况
     *
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public APIResponse list(@RequestParam(defaultValue = "0") int offset, @RequestParam(defaultValue = "10") int limit,
            @RequestParam(defaultValue = "0") int category) {
        Map<String, Object> params = Maps.newHashMap();

        CollegeType collegeType = CollegeType.codeOf(category);
        if (collegeType != null) {
            params.put("category", collegeType);
        }
        Pagination<School> pagination = schoolService.querySchools(offset, limit, params);
        return APIResponse.returnSuccess(pagination);
    }

    @RequestMapping("info")
    @ResponseBody
    public APIResponse info(@RequestParam(defaultValue = "0") int schoolId) {
        School school = schoolService.queryById(schoolId);
        if (school == null) {
            return APIResponse.returnFail("根据ID:为" + schoolId + "找不到学校");
        }
        List<Article> articleList = articleService.queryProjectBySchool(schoolId);
        school.setProjectList(articleList);
        return APIResponse.returnSuccess(school);
    }

    @RequestMapping("projects")
    @ResponseBody
    public APIResponse projects(@RequestParam(defaultValue = "0") int schoolId) {
        List<Article> articleList = articleService.queryProjectBySchool(schoolId);
        return APIResponse.returnSuccess(articleList);
    }

}
